﻿using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MqttAnalyze.Data.Common
{
    public class LogHelper
    {
        #region 成员变量
        private static string s_PathToContentRoot = string.Empty;
        private static Logger loginfo = null;
        private static readonly string C_ConfigFilePath = "nlog.config";

        #endregion


        #region public方法

        public static void Init()
        {
            try
            {
                s_PathToContentRoot = Directory.GetCurrentDirectory();
                var configFile = Path.Combine(s_PathToContentRoot, C_ConfigFilePath);
                LogManager.Setup().LoadConfigurationFromFile(configFile);
                loginfo = LogManager.GetCurrentClassLogger(); ;
            }
            catch (Exception ex)
            {
                string exinfo = ex.Message;
            }
        }
        public static void Info(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }
        public static void Info(string excptionType, string message, string stackTrace)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info("{0}:{1}\n{2}", excptionType, message, stackTrace);
            }
        }
        public static void Warn(string info)
        {
            if (loginfo.IsWarnEnabled)
            {
                loginfo.Warn(info);
            }
        }
        /// <summary>
        /// Error 要将信息写入数据表,在transaction.rollback后写入
        /// </summary>
        /// <param name="se"></param>
        public static void Error(Exception se)
        {
            if (loginfo.IsErrorEnabled && se != null)
            {
                loginfo.Error(se);
            }
        }
        public static void Error(object message)
        {
            if (loginfo.IsErrorEnabled && message != null)
            {
                loginfo.Error(message);
            }
        }
        public static void Error(Exception se,string info)
        {
            if (loginfo.IsErrorEnabled && se != null)
            {
                loginfo.Error(se, info);
            }
        }

        #endregion
    }


    public class czErrorMessage
    {
        public string From;
        public string MsgText;
    }
}
